package com.cn.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.cn.bean.Provider;
import com.cn.bean.Servicecatalog;
import com.cn.bean.Servicetype;
import com.cn.dao.impl.BaseHibernateDAOImpl;
import com.cn.service.ProviderService;
import com.cn.service.ServicecatalogService;
import com.cn.service.ServicetypeService;
import com.opensymphony.xwork2.ActionContext;

public class ServicetypeAction extends BaseHibernateDAOImpl{
	private Servicetype servicetype;
	private Provider provider;
	public Provider getProvider() {
		return provider;
	}
	public void setProvider(Provider provider) {
		this.provider = provider;
	}
	public ProviderService getProviderService() {
		return providerService;
	}
	public void setProviderService(ProviderService providerService) {
		this.providerService = providerService;
	}

	private ProviderService providerService;
	private ServicetypeService servicetypeService = null;
	public Servicetype getServicetype() {
		return servicetype;
	}
	public void setServicetype(Servicetype servicetype) {
		this.servicetype = servicetype;
	}
	public ServicetypeService getServicetypeService() {
		return servicetypeService;
	}
	public void setServicetypeService(ServicetypeService servicetypeService) {
		this.servicetypeService = servicetypeService;
	}
	
	private Servicecatalog servicecatalog;//catalog
	public Servicecatalog getServicecatalog() {
		return servicecatalog;
	}
	public void setServicecatalog(Servicecatalog servicecatalog) {
		this.servicecatalog = servicecatalog;
	}
	private ServicecatalogService servicecatalogService=null;
	
	public ServicecatalogService getServicecatalogService() {
		return servicecatalogService;
	}
	public void setServicecatalogService(ServicecatalogService servicecatalogService) {
		this.servicecatalogService = servicecatalogService;
	}
	public  String listAllType ()
	{
		Map request = (Map) ActionContext.getContext().get("request");
		
		if(servicetypeService.listAllType() != null){
			
			List serviceTypeList=servicetypeService.listAllType();
			List []serviceCatalogList=new List[serviceTypeList.size()];
			for(int i=0;i<serviceTypeList.size();i++)
			{
				serviceCatalogList[i]=servicecatalogService.listAllCatalog(i+1);//获取所有catalog
			}
			
			
			Map <String,String> mapType =new HashMap<String,String>();
			//Map <String,String> mapCatalog=new HashMap<String,String>();
			Map <String,String> [] mapCatalog=new Map[serviceTypeList.size()];
			
			for(int i=0;i<serviceTypeList.size();i++)
			{
				Servicetype st=(Servicetype)serviceTypeList.get(i);
				mapType.put(st.getId().toString(), st.getName());
			}
			for(int k=0;k<serviceTypeList.size();k++)
			{
				mapCatalog[k]=new HashMap<String,String>();
			for(int j=0;j<serviceCatalogList[k].size();j++)
			{
				Servicecatalog sc=(Servicecatalog)serviceCatalogList[k].get(j);
				
				mapCatalog[k].put(sc.getId().toString(), sc.getName());
				System.out.println("正在为mapCatalog["+k+"]填充第"+j+"组数据");
			}
			request.put("serviceCatalogList"+String.valueOf(k+1),mapCatalog[k]);
			System.out.println("正在为serviceCatalogList"+String.valueOf(k+1)+"填入mapCatalog["+k+"]");
			}
			request.put("serviceTypeList", mapType);  

			
			Map session = (Map) ActionContext.getContext().get("session");
			provider = (Provider) session.get("provider");
			List list=new ArrayList();
			list=providerService.showServicePrice(provider.getId());
			if(list.get(0)!=null)
				session.put("priceList", list);
			
			return "success";
		}else{
			return "fail";
		}
	}
}
